import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [{
        path: '/login',
        name: 'login',
        component: () =>
            import ('@/views/Login.vue')
    }, {
        path: '/',
        redirect: '/login'

    }, {
        path: '/home',
        component: () =>
            import ('@/views/Home.vue'),
        redirect: '/index',
        meta: {
            title: '首页'
        },
        children: [{
                path: '/index',
                name: 'index',
                component: () =>
                    import ('@/views/Index.vue'),

            },
            {
                path: '/menu',
                name: 'menu',
                component: () =>
                    import ('@/views/Menu.vue'),
                meta: {
                    title: '分类管理'
                }
            },
            {
                path: '/article',
                name: 'article',
                component: () =>
                    import ('@/views/Article/Article.vue'),
                meta: {
                    title: '文章列表'
                }
            },
            {
                path: '/addarticle',
                name: 'addarticle',
                component: () =>
                    import ('@/views/Article/ArticleAdd.vue'),
                meta: {
                    title: '添加文章'
                }
            },
            {
                path: '/article/:id',
                name: 'articleEdit',
                component: () =>
                    import ('@/views/Article/ArticleEdit.vue'),
                meta: {
                    title: '修改文章'
                }
            },
            {
                path: '/advimg',
                name: 'advimg',
                component: () =>
                    import ('@/views/Adv/Advimg.vue'),
                meta: {
                    title: '广告图管理'
                }
            },
            {
                path: '/advpos',
                name: 'advpos',
                component: () =>
                    import ('@/views/Adv/Advpos.vue'),
                meta: {
                    title: '广告位管理'
                }
            },
            {
                path: '/link',
                name: 'link',
                component: () =>
                    import ('@/views/Link.vue'),
                meta: {
                    title: '友情链接管理'
                }
            },
            {
                path: '/webset',
                name: 'webset',
                meta: {
                    title: '网站设置'
                },
                component: () =>
                    import ('@/views/Web/SetWeb.vue')
            },
            {
                path: '/tag',
                name: 'tag',
                component: () =>
                    import ('@/views/Tag.vue'),
                meta: {
                    title: '标签管理'
                }
            },
            {
                path: '/setArtilce',
                name: 'setArtilce',
                component: () =>
                    import ('@/views/Web/setArtilce.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/adminQx',
                name: 'adminQx',
                component: () =>
                    import ('@/views/admin/adminQx.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/adminList',
                name: 'adminList',
                component: () =>
                    import ('@/views/admin/adminList.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/userList',
                name: 'userList',
                component: () =>
                    import ('@/views/user/userList.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/userAdd',
                name: 'userAdd',
                component: () =>
                    import ('@/views/user/userAdd.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/videoAdd',
                name: 'videoAdd',
                component: () =>
                    import ('@/views/video/videoAdd.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/video',
                name: 'video',
                component: () =>
                    import ('@/views/video/video.vue'),
                meta: {
                    title: '404'
                }
            },
            {
                path: '/404',
                component: () =>
                    import ('@/views/Error')
            },
        ]
    }

]


const router = new VueRouter({
    routes
})

// 判断是否可以有权限登录
router.beforeEach((to, from, next) => {
    if (['/', '/login'].includes(to.path)) {
        next()
    } else {
        if (localStorage.getItem('userinfo')) {
            next()
        } else {
            next('/login')
        }
    }
})

export default router